﻿namespace EFMLEditor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;

    using FastColoredTextBoxNS;

    public partial class CodePage : UserControl
    {
        private readonly Document d;

        public CodePage(Document d)
        {
            this.d = d;
            this.InitializeComponent();

            var menu = new AutocompleteMenu(this.fastColoredTextBox1);
            var items = new List<AutocompleteItem>();

           // items.Add(new AutocompleteItem("document"));
            //items.Add(new MethodAutocompleteItem2("document.GetElementById()"));

            this.TypeComplete(items, typeof(Creek.UI.EFML.Base.JS.ScriptingTypes.Document));

            menu.Items.SetAutocompleteItems(items);
        }

        public void TypeComplete(List<AutocompleteItem> items, Type t)
        {
            items.Add(new AutocompleteItem(t.Name));
            items.AddRange(t.GetMethods().Select(m => new MethodAutocompleteItem2(t.Name + "." + m.Name + "()")));
            items.AddRange(t.GetProperties().Select(m => new MethodAutocompleteItem2(t.Name + "." + m.Name + "")));
        }

        private void fastColoredTextBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            Application.isDirty = true;
            foreach (var document in Application.Strings.Where(document => document.Name == this.d.Name))
            {
                document.Value = this.fastColoredTextBox1.Text;
            }
        }
    }
}